import useSWR from 'swr';
import { cg06WarehouseDataApi } from '../api/newWmsList';
import { useAuthStore } from '@/app/cg06/store/useAuthStore';

export const useNewBarData = () => {
    const token = useAuthStore(s => s.token);
	const { data, error, isLoading } = useSWR(
		() => (token ? ' /big-screen/screen/warehouseData/list' : null),
		cg06WarehouseDataApi,
		{revalidateOnFocus: false}
    );




	/**
	 * 在库库存分布
	 */

	const onwayInventoryBarData: {
		yAxis: string[];
		seriesData: {
		  name: string;
		  data: number[];
		  labelColor: string;
		}[];
	  } = {
		yAxis: [],
		seriesData: [
		  {
			name: '',
			data: [],
			labelColor: '#88D0FF',
		  }
		],
	};

    const showWarehouseList = data?.filter((i: any) => !!i.whShowFlag).slice(-6) || [];
	
	onwayInventoryBarData.yAxis = showWarehouseList.map((i: any) => i.warehouseName) || [];
	onwayInventoryBarData.seriesData[0].data = showWarehouseList.map((i: any) => i.onwayNums) || [];

	/**
	 * 仓库库存分布
	 */

	const warehouseInventoryBarData = {
		yAxis: [],
		seriesData: [
			{
				name: '',
				data: [],
				labelColor: '#88D0FF'
			}
		]
	};


	warehouseInventoryBarData.yAxis = showWarehouseList.map((i: any) => i.warehouseName) || [];
	warehouseInventoryBarData.seriesData[0].data = showWarehouseList.map((i: any) => i.inventoryNums) || [];

	/**
	 * 仓库出库分布
	 */

	const warehouseShipmentBarData = {
		yAxis: [],
		seriesData: [
			{
				name: '',
				data: [],
				labelColor: '#88D0FF'
			}
		]
	};

	warehouseShipmentBarData.yAxis = showWarehouseList.map((i: any) => i.warehouseName) || [];
	warehouseShipmentBarData.seriesData[0].data = showWarehouseList.map((i: any) => i.shipmentNums) || [];

	
	
	 
	

	
	return {
		onwayInventoryBarData: onwayInventoryBarData,
		warehouseInventoryBarData: warehouseInventoryBarData,
		warehouseShipmentBarData: warehouseShipmentBarData,
		isLoading,
		isError: error
	};
};

